package com.dhecp.core.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dhecp.project.api.util.ApiConfig;
import okhttp3.*;
import java.io.File;
import java.util.List;

/**
 * 大森林请求工具
 */
public class FroestHttPUtils {


    /**
     * 登录接口请求
     */
    public static JSONObject froestLogin(){
        JSONObject jsonObject;
        try {
            //判断是否需要登录
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                    .addFormDataPart("username",ApiConfig.getWmsAccount())
                    .addFormDataPart("password",ApiConfig.getWmsPassword())
                    .addFormDataPart("loginType","2")
                    .build();
            Request request = new Request.Builder()
                    .url(ApiConfig.getWmsUrl()+"/oauth/login")
                    .method("POST", body)
                    .addHeader("client_id", "forest")
                    .addHeader("client_secret", "Forest")
                    .addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
                    .build();
            Response response = client.newCall(request).execute();
            String result = response.body().string();
            jsonObject = JSON.parseObject(result);
        }catch (Exception e){
            jsonObject = new JSONObject();
            jsonObject.put("code",500);
            jsonObject.put("message",e.getMessage());
        }
        return jsonObject;
    }

    /**
     *API请求
     */
    public static JSONObject froestApiRequest(String content,String authorization,String url){
        JSONObject jsonObject;
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, content);
            Request request = new Request.Builder()
                    .url(url)
                    .method("POST", body)
                    .addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
                    .addHeader("Content-Type", "application/json")
                    .addHeader("Authorization", authorization)
                    .build();
            Response response = client.newCall(request).execute();
            String str = response.body().string();
            jsonObject = JSON.parseObject(str);
        }catch (Exception e){
            jsonObject = new JSONObject();
            jsonObject.put("code",500);
            jsonObject.put("message",e.getMessage());
        }
        return jsonObject;
    }

    /**
     *创建出库单
     */
    public static JSONObject createOutboundOrder(String content, String authorization, String url, List<String> filePaths){
        JSONObject jsonObject;
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
            for (String filePath : filePaths) {
                File file = new File(filePath);
                builder.addFormDataPart("multipartFile", file.getName(), RequestBody.create(MediaType.parse("application/octet-stream"), file));
            }
            builder.addFormDataPart("wareOutboundDeliveryList","wareOutboundDeliveryList",
                            RequestBody.create(MediaType.parse("application/json"),
                                    content));
            RequestBody body = builder.build();
            Request request = new Request.Builder()
                    .url(ApiConfig.getWmsUrl() + url)
                    .method("POST", body)
                    .addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
                    .addHeader("Authorization", authorization)
                    .addHeader("Content-Type", "application/json")
                    .build();
            Response response = client.newCall(request).execute();
            String str = response.body().string();
            jsonObject = JSON.parseObject(str);
        }catch (Exception e){
            jsonObject = new JSONObject();
            jsonObject.put("code",500);
            jsonObject.put("message",e.getMessage());
        }
        return jsonObject;
    }
}
